import os,tempfile,shutil,commands
import pickle
import sys
import time
from crvclient import doSetUntilDone,doGetUntilDone

inf=open(sys.argv[1],"r")

vmid=pickle.load(inf)
serverstr=pickle.load(inf)
serverhostname=pickle.load(inf)

MetaDataProvided=False

while not MetaDataProvided:
    output=doGetUntilDone("%s/getVMAttr?vmid=%s&name=MetaDataProvided"%(serverstr,vmid))
    if output=="True":
        MetaDataProvided=True
    time.sleep(5)
    
#now obtain metadata
metadata=doGetUntilDone("%s/getVMAttr?vmid=%s&name=MetaData"%(serverstr,vmid))

#Metadata will be the result from "tar cz certfiles|base64 -w 0"
#save it somewhere

base64file=tempfile.gettempdir()+"metadata.tgz.base64"

ouf=open(base64file,"w")
ouf.write(metadata)
ouf.close()

try:
    os.makedirs("/etc/puppet")
except:
    pass

cmdstr="base64 -d -w 0 %s|tar zx -C /etc/puppet/"%(base64file)
(status,output)=commands.getstatusoutput(cmdstr)
print "Running command=%s, status=%d, output=%s"%(cmdstr,status,output)

